package top.z.orm.handle.osql.dialect.sqlserver;

import top.z.orm.core.osql.dialect.DbFeature;
import top.z.orm.core.osql.exception.NotSupportedException;
import top.z.orm.handle.osql.core.HoneyUtil;

import static top.z.orm.core.osql.SqlKeyWord.TOP;

public class SqlServerFeature implements DbFeature {

    public String toPageSql(String sql, int start, int size) {
        if (start > 0) {
            throw new NotSupportedException("select result did not support paging skip");
        }
        return toPageSql(sql, size);
    }

    public String toPageSql(String sql, int size) {
        sql = HoneyUtil.deleteLastSemicolon(sql);
        int           index1      = sql.toLowerCase().indexOf("select");
        int           index2      = sql.toLowerCase().indexOf("select distinct");
        int           insertIndex = index1 + (index2 == index1 ? 15 : 6);
        StringBuilder sb          = new StringBuilder(sql.length() + 6 + (size + "").length()).append(sql);
        if (HoneyUtil.isRegPagePlaceholder()) {
            int[] array = new int[1];
            array[0] = size;
            HoneyUtil.regPageNumArray(array);
            // sb.insert(insertIndex, " top ?");
            sb.insert(insertIndex, " " + TOP + " ?");
        } else {
            // sb.insert(insertIndex, " top " + size);
            sb.insert(insertIndex, " " + TOP + " " + size);
        }
        return sb.toString();
    }
}